// 1.基本数据类型

// 字符串
let name1 : string = "张三";
let message: string = `Hello, ${name1}!`;

// 数字
let age: number = 25;
let price: number = 99.99;
let binary: number = 0b1010;

// 布尔值
let isActive: boolean = true;
let isCompleted: boolean = false;

// null 和 undefined
let empty: null = null;
let notDefined: undefined = undefined;

// 2.数组类型

// 两种声明方式
let numbers: number[] = [1, 2, 3, 4];
let strings: Array<string> = ["a", "b", "c"];

console.log(numbers)
console.log(strings)

// 只读数组
let readonlyArray: ReadonlyArray<number> = [1, 2, 3];
console.log(readonlyArray)

//3.元组类型
let person: [string, number] = ["张三", 25];
let rgb: [number, number, number] = [255, 0, 0];

// 可选元组元素
let optionalTuple: [string, number?] = ["李四"];

//4.对象类型

// 接口定义对象结构
interface User {
  name: string;
  age: number;
  email?: string; // 可选属性
}

let user: User = {
  name: "王五",
  age: 30
};

// 类型别名
type Point = {
  x: number;
  y: number;
};

//5.枚举类型

// 数字枚举
enum Direction {
  Up = 1,
  Down,
  Left,
  Right
}

// 字符串枚举
enum Color {
  Red = "RED",
  Green = "GREEN",
  Blue = "BLUE"
}

// 常量枚举
const enum Size {
  Small,
  Medium,
  Large
}

//6. 任意类型
let uncertain: any = "可以是任意类型";
uncertain = 42;
uncertain = true;

//7. 未知类型
let notSure: unknown = "需要类型检查";
// notSure.toFixed(); // 错误：需要类型断言
if (typeof notSure === "number") {
  notSure.toFixed(); // 正确
}

// 8. 空类型和无返回类型
// void - 没有返回值
function logMessage(): void {
  console.log("Hello");
}

// never - 永远不会返回结果
function throwError(message: string): never {
  throw new Error(message);
}

function infiniteLoop(): never {
  while (true) {}
}

//9. 联合类型
let id: string | number = "abc123";
id = 123;

function printId(id: string | number) {
  console.log(`ID: ${id}`);
}

//10. 交叉类型
interface Named {
  name: string;
}

interface Aged {
  age: number;
}

type Person = Named & Aged;

let person1: Person = {
  name: "赵六",
  age: 28
};

//11. 字面量类型
// 字符串字面量
type Direction1 = "up" | "down" | "left" | "right";
let move: Direction1 = "up";

// 数字字面量
type Dice = 1 | 2 | 3 | 4 | 5 | 6;
let roll: Dice = 6;

// 布尔字面量
type Truthy = true;
let isTrue: Truthy = true;


// 12. 函数类型
// 函数声明
type AddFunction = (a: number, b: number) => number;

const add: AddFunction = (x, y) => x + y;

// 函数重载
function reverse(value: string): string;
function reverse(value: number): number;
function reverse(value: string | number): string | number {
  if (typeof value === "string") {
    return value.split("").reverse().join("");
  }
  return Number(value.toString().split("").reverse().join(""));
}

//13. 泛型
// 泛型函数
function identity<T>(arg: T): T {
  return arg;
}

// 泛型接口
interface GenericArray<T> {
  [index: number]: T;
}

// 泛型类
class GenericNumber<T> {
  zeroValue!: T;
  add!: (x: T, y: T) => T;
}


//类型断言
// 两种语法
let someValue: any = "这是一个字符串";
let strLength1: number = (someValue as string).length;
let strLength2: number = (<string>someValue).length;